const express = require('express');
const router = express.Router();
const con = require('../db');
const con2 = require('../db/mysql');
const md5 = require('md5');

// router.get('/', (req, res) => {
//     res.send("1")
// });
router.post('/login', (req, res) => {
    const { username, password } = req.body;
    con.query(`SELECT * FROM manager WHERE username=? AND password=?`, [username, md5(password)], (err, result) => {
        if (err) throw err;
        if (result.length > 0) {
            res.json({
                msg: "登录成功",
                code: 200,
                id: result[0].id,
                rid:result[0].rid
            })
        } else {
            res.json({
                msg: "登录失败",
                code: 500
            })
        }

    })
})
router.put('/password', (req, res) => {
    const { id, password, newpass } = req.body;
    con.query(`SELECT * FROM manager WHERE id=? AND password=?`, [id, md5(password)], (err, result) => {
        if (err) throw err;
        if (result.length > 0) {
            con.query(`UPDATE manager SET password=? WHERE id=?`, [md5(newpass), id], (err, result) => {
                if (err) throw err;
                if (result.affectedRows === 1) {
                    res.json({
                        msg: "更新成功",
                        code: 200
                    })
                } else {
                    res.json({
                        msg: "更新失败",
                        code: 500
                    })
                }
            })
        } else {
            res.json({
                msg: "原密码错误",
                code: 500
            })
        }
    })
})
router.get('/findbyrid', (req, res) => {
    const { rid } = req.query;
    con.query(`SELECT * FROM manager WHERE rid=?`, rid, (err, result) => {
        if (err) throw err;
        if (result.length) {
            res.json({
                msg: "当前角色有用户",
                code: 201
            })
        } else {
            res.json({
                msg: "当前角色无用户",
                code: 202
            })
        }
    })
})
router.get('/', async (req, res) => {
    const { page, limit, username, rid } = req.query;
    try {
        let where = "";
        if (rid) {
            where += ` AND manager.rid = ${rid}`
        }
        if (username) {
            where += ` AND username LIKE '%${username}%' `
        }
        const result = await con2.query(`SELECT manager.id,manager.username,role.rolename FROM  manager,role WHERE manager.rid=role.id ${where} ORDER BY manager.id DESC LIMIT ${limit} OFFSET ${page * limit - limit};SELECT COUNT(*) AS total FROM manager WHERE 1 ${where}`);
        if (result.length) {
            res.json({
                msg: "查询成功",
                code: 200,
                data: result[0],
                total: result[1][0].total
            });
        }
    } catch (error) {
        throw error;
    }
})
router.post('/', async (req, res) => {
    const { username, rid } = req.body;
    try {
        const result = await con2.query(`INSERT INTO manager (username,password,rid) VALUES (?,?,?)`, [username, md5("112233"), rid]);
        if (result.affectedRows === 1) {
            res.json({
                msg: "新增成功",
                code: 200,
            })
        } else {
            res.json({
                msg: "新增失败",
                code: 500,
            })
        }
    } catch (error) {
        throw error;
    }
})
router.delete('/:id', async (req, res) => {
    const { id } = req.params;
    try {
        const result = await con2.query(`DELETE FROM manager WHERE id=?`, id);
        if (result.affectedRows === 1) {
            res.json({
                msg: "删除成功",
                code: 200,
            })
        } else {
            res.json({
                msg: "删除失败",
                code: 500,
            })
        }
    } catch (error) {
        throw error;
    }
})
router.get('/:id', async (req, res) => {
    const { id } = req.params;
    try {
        const result = await con2.query(`SELECT * FROM manager WHERE id=?`, id);
        if (result.length) {
            res.json({
                msg: "查询成功",
                code: 200,
                data: result[0]
            })
        } else {
            res.json({
                msg: "查询失败",
                code: 500,
            })
        }
    } catch (e) {
        throw e;
    }
})
router.put('/:id', async (req, res) => {
    const { id } = req.params;
    const { rid, username, password } = req.body;
    try {
        let result;
        if (password !== "") {
            result = await con2.query(`UPDATE manager SET rid=?,username=?,password=? WHERE id=?`, [rid, username, md5(password), id]);

        } else {
            result = await con2.query(`UPDATE manager SET rid=?,username=? WHERE id=?`, [rid, username, id]);
        }
        if (result.changedRows === 1) {
            res.json({
                msg: "修改成功",
                code: 200,
            })
        } else {
            res.json({
                msg: "未修改任何数据或修改失败",
                code: 500,
            })
        }

    } catch (error) {
        throw error;
    }
})
module.exports = router;